﻿using System;
using System.Collections.Generic;
using Engine;
using Game;
using Mekiasm.Common;
using Mekiasm.ElectricElement;

namespace Mekiasm
{
    public class PrecisionSawmill : MachineRotateAbleItem,Interface.IDynamicCraft
    {
        public static int Index;

        public PrecisionSawmill() : base("精密锯木机")
        {
            
            textureSlots = new int[] { 126, 127, 119, 127, 127, 95 };
        }

        public int[] CraftSlotIndies => new int[] { 0};

        public int[] ResultSlotIndies => new int[] { 1, 2 };

        public int[] RemainSlotIndies => new int[0];

        public int SlotsCount => 3;

        public int BlockValue => blockValue;

        public Point2 CraftSize => new Point2(1);

        public int[] FuelCraftSlotIndies => new int[0];

        public void GenerateRecipes(List<MekRecipe> mekRecipe)
        {
            mekRecipe.Add(new MekRecipe(1, 0, 2).SetName("木板").SetIngredian(0, ILibrary.GetCraftId(BirchWoodBlock.Index)).SetResult(0, SawDust.Index).SetResult(1, PlanksBlock.Index, 6));
            mekRecipe.Add(new MekRecipe(1, 0, 2).SetName("木板").SetIngredian(0, ILibrary.GetCraftId(RubberWood.Index)).SetResult(0, SawDust.Index).SetResult(1, PlanksBlock.Index, 6));
        }

        public void GenerateRecipeWidget(CanvasWidget canvas, ComponentEnergyMachine energyMachine, ComponentMiner miner, ref Action Tick)
        {
            var progressWidget = new MekProgressWidget(MekProgressWidget.ProgessType.large_right) { Size = new Vector2(96, 16), Margin = new Vector2(10, 0), VerticalAlignment = WidgetAlignment.Center };
            var stackHori1 = new StackPanelWidget() { Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
            var stackVerti1 = new StackPanelWidget() { Direction = LayoutDirection.Vertical };
            var craft = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(10, 0), VerticalAlignment = WidgetAlignment.Center };
            var result1 = new InventorySlotWidget() { Size = new Vector2(48, 48) };
            var result2 = new InventorySlotWidget() { Size = new Vector2(48, 48), Margin = new Vector2(0, 10) };
            result1.Children.Add(new LabelWidget() { Text = "...", Color = new Color(0, 0, 0, 96) });
            craft.AssignInventorySlot(energyMachine, 0);
            result1.AssignInventorySlot(energyMachine, 1);
            result2.AssignInventorySlot(energyMachine, 2);
            stackHori1.AddChildren(craft);stackHori1.AddChildren(progressWidget);stackHori1.AddChildren(stackVerti1);
            stackVerti1.AddChildren(result1);stackVerti1.AddChildren(result2);
            canvas.AddChildren(stackHori1);
            Tick += () => {
                progressWidget.SetProgress(energyMachine.CraftProgress);
            };

        }

        public override MekBlock.DeviceType GetDeviceType()
        {
            return MekBlock.DeviceType.Electric;
        }
        public override void OnBlockAdded(SubsystemBlockBehavior blockBehavior, int value, int oldValue, int x, int y, int z)
        {
            ILibrary.AddBaseEnergyEntity(DisplayName, x, y, z,
                new List<ComponentEnergyMachine.Slot>() { new ComponentEnergyMachine.Slot(), new ComponentEnergyMachine.Slot(), new ComponentEnergyMachine.Slot() },
                ILibrary.GetRotetedConfig(value, new MachineFaceConfig[] {
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Electric),
            new MachineFaceConfig(MekBlock.MachineMode.IN,MekBlock.DeviceType.Item),
            new MachineFaceConfig(MekBlock.MachineMode.OUT,MekBlock.DeviceType.Item)
            }));
            ElectricSystem.Add(new PrecisionSawmillElement(x, y, z));
        }
        public override void OnBlockGenerated(int value, int x, int y, int z, bool isLoaded)
        {
            ElectricSystem.Add(new PrecisionSawmillElement(x, y, z));
        }
        public override void OnBlockRemoved(SubsystemBlockBehavior blockBehavior, int value, int newValue, int x, int y, int z)
        {
            ILibrary.RemoveBlockEntity(x, y, z);
            ElectricSystem.Remove(new Point3(x, y, z));
        }
        public override bool OnInteract(SubsystemBlockBehavior blockBehavior, TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
        {
            ComponentPrecisionSawmill mcomponent = ILibrary.SubsystemBlockEntities.GetBlockEntity(raycastResult.CellFace.X, raycastResult.CellFace.Y, raycastResult.CellFace.Z).Entity.FindComponent<ComponentPrecisionSawmill>(true);
            componentMiner.ComponentPlayer.ComponentGui.ModalPanelWidget = new PrecisionSawmillWidget(mcomponent, componentMiner);
            return true;
        }
    }
}
